package com.spzx.product.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageHelper;
import com.spzx.common.core.constant.SecurityConstants;
import com.spzx.common.core.domain.R;
import com.spzx.common.core.web.controller.BaseController;
import com.spzx.common.core.web.domain.AjaxResult;
import com.spzx.common.core.web.page.TableDataInfo;
import com.spzx.common.log.annotation.Log;
import com.spzx.common.security.annotation.InnerAuth;
import com.spzx.product.domain.*;
import com.spzx.product.mapper.ProductDetailsMapper;
import com.spzx.product.mapper.ProductMapper;
import com.spzx.product.mapper.ProductSkuMapper;
import com.spzx.product.mapper.SkuStockMapper;
import com.spzx.product.service.ProductService;
import com.spzx.product.service.ProductSkuService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 商品Controller
 */
@Tag(name = "商品接口管理")
@RestController
@RequestMapping("/product")
public class ProductController extends BaseController {

    @Resource
    private ProductService productService;
    @Autowired
    ProductMapper productMapper;
    @Autowired
    ProductSkuMapper productSkuMapper;
    @Autowired
    ProductDetailsMapper productDetailsMapper;
    @Autowired
    SkuStockMapper skuStockMapper;
    @Autowired
    ProductSkuService productSkuService;

    @Operation(summary = "批量获取商品sku最新价格信息")
    @InnerAuth
    @PostMapping("/getSkuPriceList")
    public R<List<SkuPrice>> getSkuPriceList(@RequestBody List<Long> skuIdList){
        List<SkuPrice> skuPriceList = productSkuService.selectAllSkuPrice(skuIdList);
        return R.ok(skuPriceList);
    }

    @InnerAuth
    @GetMapping("/getProductSku/{skuId}")
    public R<ProductSku> getProductSku(@PathVariable("skuId")Long skuId){
        System.out.println("调用productService业务层，查询sku");
        ProductSku productSku = productService.getProductSkuById(skuId);
        return R.ok(productSku);
    }

    @InnerAuth
    @GetMapping("/getProduct/{productId}")
    R<Product> getProduct(@PathVariable("productId" )Long productId){
        System.out.println("根据productId查询product");
        Product product = productMapper.selectById(productId);
        return R.ok(product);
    }

    @InnerAuth
    @GetMapping("/getSkuPrice/{productId}")
    R<SkuPrice> getSkuPrice(@PathVariable("productId" )Long productId){
        System.out.println("根据productId查询sku价格");
        ProductSku productSku = productSkuMapper.selectById(productId);
        SkuPrice skuPrice = new SkuPrice();
        skuPrice.setSkuId(productSku.getId());
        skuPrice.setMarketPrice(productSku.getMarketPrice());
        skuPrice.setSalePrice(productSku.getSalePrice());
        return R.ok(skuPrice);
    }

    @InnerAuth
    @GetMapping("/getDetailsimagesUrlList/{productId}")
    R<ProductDetails> getDetailsimagesUrlList(@PathVariable("productId" )Long productId){
        System.out.println("根据productId查询商品图片");
        ProductDetails productDetails = productDetailsMapper.selectById(productId);
        return R.ok(productDetails);
    }

    @InnerAuth
    @GetMapping("/getSkuStockVo/{skuId}")
    R<SkuStockVo> getSkuStockVo(@PathVariable("skuId" )Long skuId){
        System.out.println("根据skuId查询商品库存");
        SkuStock skuStock = skuStockMapper.selectById(skuId);
        SkuStockVo skuStockVo = new SkuStockVo();
        skuStockVo.setSkuId(skuStock.getSkuId());
        skuStockVo.setAvailableNum(skuStock.getAvailableNum());
        skuStockVo.setSaleNum(skuStock.getSaleNum());
        return R.ok(skuStockVo);
    }


    @InnerAuth
    @GetMapping("/getskuSpecValueMap/{productId}")
    Map<String, Long> getskuSpecValueMap(@PathVariable("productId" )Long productId){
        System.out.println("商品规格对应商品skuId信息");
        QueryWrapper<ProductSku> productSkuQueryWrapper = new QueryWrapper<>();
        productSkuQueryWrapper.eq("product_id", productId);
        List<ProductSku> productSkus = productSkuService.list(productSkuQueryWrapper);
        Map<String, Long> collect = productSkus.stream().collect(Collectors.toMap(ProductSku::getSkuSpec, ProductSku::getId));
        return collect;
    }

    @Operation(summary = "获取销量好的sku")
    @GetMapping("getTopSale")
    public R<List<ProductSku>> getTopSale() {
        return R.ok(productService.getTopSale());
    }

    //添加
    @PostMapping("/add")
    public AjaxResult add(@RequestBody Product product) {
        return toAjax(productService.insertProduct(product));
    }

    /**
     * 查询商品列表
     */
    @Operation(summary = "查询商品列表")
    @GetMapping("/list")
    public TableDataInfo list(Product product) {
        startPage();
        List<Product> list = productService.selectProductList(product);
        return getDataTable(list);
    }

    /**
     * 获取商品详细信息
     */
    @Operation(summary = "获取商品详细信息")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(productService.selectProductById(id));
    }

    /**
     * 修改商品
     * @param product
     * @return
     */
    @Operation(summary = "修改商品")
    @PutMapping
    public AjaxResult edit(@RequestBody Product product) {
        return toAjax(productService.updateProduct(product));
    }

    /**
     * 删除商品
     *
     * @param ids
     * @return
     */
    @Operation(summary = "删除商品")
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(productService.deleteProductByIds(ids));
    }

    //远程调用使用，商品列表
    @GetMapping("/skuList/{pageNum}/{pageSize}")
    public R<TableDataInfo> skuList(@PathVariable Integer pageNum,
                                    @PathVariable Integer pageSize,
                                    @ModelAttribute SkuQuery skuQuery) {
        //设置分页参数
        PageHelper.startPage(pageNum,pageSize);
        //调用service方法根据条件查询
        List<ProductSku> list = productService.selectProductSkuList(skuQuery);

        TableDataInfo dataTable = getDataTable(list);
        return R.ok(dataTable);
    }

}
